package tu.java.hall;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import tu.java.db.DBConnManager;
import tu.java.db.DBUtil;

public class Hall {
	
	
	
	private int					hallId;
	//private int					hallSeatCount;
	private String				hallName;
	private String				hallDescription;
	private Vector<HallSeat>	hallSeats;
	
	
	
	public Hall(String hallName, String hallDescription/*, int hallSeatCount*/) {
		this.hallName = hallName;
		this.hallDescription = hallDescription;
		//this.hallSeatCount = hallSeatCount;
	}
	
	
	
	public Hall() {
	}
	
	
	
	public boolean addHall(int userId) {
		
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection(false);
			pstmt = conn.prepareStatement("INSERT INTO TH01_HALLS (CH01_HALL_NAME,CH01_HALL_DESCRIPTION,CS02_USER_ID) VALUES (?,?,?)");
			pstmt.setString(1, hallName);
			pstmt.setString(2, hallDescription);
			pstmt.setInt(3, userId);
			pstmt.executeUpdate();
			
			pstmt = conn.prepareStatement("SELECT TH01_HALLS.CURRVAL FROM DUAL");
			rs = pstmt.executeQuery();
			if (rs.next()) {
				hallId = rs.getInt(1);
			}
			
/*			for (int i = 0; i < hallSeatCount; i++) {
				HallSeat.addHallSeat(conn, pstmt, i, hallId);
			}*/
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBUtil.commit(conn, status);
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public static Vector<Hall> searchHalls(String hallName, int hallId) {
		
		Vector<Hall> halls = new Vector<Hall>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" SELECT CH01_HALL_ID,CH01_HALL_NAME,CH01_HALL_DESCRIPTION FROM TH01_HALLS");
			if (hallId != 0) {
				sql.append(" WHERE CH01_HALL_ID=?");
			}
			else {
				sql.append(" WHERE CH01_HALL_NAME LIKE ?");
			}
			
			pstmt = conn.prepareStatement(sql.toString());
			if (hallId != 0) {
				pstmt.setInt(1, hallId);
			}
			else {
				pstmt.setString(1, hallName);
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Hall h = new Hall();
				h.hallId = rs.getInt(1);
				h.hallName = rs.getString(2);
				h.hallDescription = rs.getString(3);
				h.hallSeats = HallSeat.loadAllSeats(h.hallId);
				halls.add(h);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return halls;
	}
	
	
	
	public boolean editHall(int userId) {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" UPDATE TH01_HALLS SET CH01_HALL_NAME=?,CH01_HALL_DESCRIPTION=?,CS02_USER_ID=? WHERE CH01_HALL_ID=?");
			pstmt.setString(1, hallName);
			pstmt.setString(2, hallDescription);
			pstmt.setInt(3, userId);
			pstmt.setInt(4, hallId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	public boolean deleteHall() {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" DELETE FROM TH01_HALLS WHERE CH01_HALL_ID=?");
			pstmt.setInt(1, hallId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	public int getHallSeatCount() {
		int seatCount = 0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" select count(CH01_HALL_ID) from TH02_SEATS");
			sql.append(" where CH01_HALL_ID = ?");
			
			pstmt = conn.prepareStatement(sql.toString());
			
			pstmt.setInt(1, hallId);

			rs = pstmt.executeQuery();
			
			if (rs.next()) {
				seatCount = rs.getInt(1);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return seatCount;
	}
	
	
	public int getHallId() {
		return hallId;
	}
	
	
	
	public void setHallId(int hallId) {
		this.hallId = hallId;
	}
	
	
	
	public String getHallDescription() {
		return hallDescription;
	}
	
	
	
	public void setHallDescription(String hallDescription) {
		this.hallDescription = hallDescription;
	}
	
	
	
	public Vector<HallSeat> getHallSeats() {
		return hallSeats;
	}
	
	
	
	public void setHallSeats(Vector<HallSeat> hallSeats) {
		this.hallSeats = hallSeats;
	}
	
	
	
/*	public int getHallSeatCount() {
		return hallSeatCount;
	}*/
	
	
	
/*	public void setHallSeatCount(int hallSeatCount) {
		this.hallSeatCount = hallSeatCount;
	}*/
	
	
	
	public String getHallName() {
		return hallName;
	}
	
	
	
	public void setHallName(String hallName) {
		this.hallName = hallName;
	}
	
}
